package com.xcl.controller;

import com.xcl.pojo.Category;
import com.xcl.pojo.Result;
import com.xcl.service.CategoryService;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
//@CrossOrigin  //支持跨域访问
@Validated
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    //查询所有分类  /category/findAll
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Category> lst = categoryService.findAll();
        return Result.success(lst);
    }


    //删除
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        Integer r = categoryService.deleteById(id);
        if(r > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }


    @RequestMapping("/addCategory")
    public Result addCategory(@Valid @RequestBody Category category){
        Integer r = categoryService.addCategory(category);
        if(r > 0){
            return Result.success();
        }else {
            return Result.error("增加失败");
        }

    }


    //修改
    @RequestMapping("/update")
    public Result update(@Valid @RequestBody Category category){
        Integer r = categoryService.update(category);
        if(r > 0){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

}
